﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tong;
public partial class aspnet_admin_bock_Column_CX_Column_HankAdd : System.Web.UI.Page
{
    /// <summary>
    /// 数据操作通用类 实例 TextData
    /// </summary> 
    public TongUse TextData = new TongUse();

    /// <summary>
    /// 系统安装绝对路径
    /// </summary>
    public string Abs_Path = TongUse.SysPath;
    /// <summary>
    /// 用户存放数据的临时表
    /// </summary>
    DataTable MyDt = new DataTable();

    /// <summary>
    /// 站点通用方法类
    /// </summary>
    public Tong.ThisSiteUse.SiteTong TextThisSite = new Tong.ThisSiteUse.SiteTong();
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "批量添加栏目页面";
        Button1.Attributes.Add("onclick", "loadinxxxx();");
        
    }


    /// <summary>
    /// 添加栏目的临时级别
    /// </summary>
    int GetXmlChildsOutTreeViewCloumnLvTemp = 0;

    /// <summary>
    /// 添加栏目的临时ID
    /// </summary>
    int GetXmlChildsOutTreeViewColumnsIDTemp = 0;

    /// <summary>
    /// 遍历XML文件  添加栏目函数
    /// </summary>
    /// <param name="XmlData">XML文件内容</param>
    /// <param name="WantCloumnLv">要添加的栏目级别</param>
    /// <param name="ColumnID">添加的栏目ＩＤ</param>
    /// <param name="NewsParitemp">如果要添加新闻栏目的话 则此项不能为空 不设置为"" 空</param>
    /// <returns></returns>
    public string GetXmlChildsOutTreeView(String XmlData, int WantCloumnLv, int ColumnID, string NewsParitemp)
    {
        System.Xml.XmlDataDocument MyXml = new System.Xml.XmlDataDocument();

        MyXml.LoadXml(XmlData);
        for (int i = 0; i < MyXml.DocumentElement.ChildNodes.Count; i++)
        {
            try
            {   //只能添加一个级别的栏目
                if (GetXmlChildsOutTreeViewCloumnLvTemp == 0)
                {
                    // Response.Write(TextData.EStr(GetXmlChildsOutTreeViewCloumnLvTemp, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") + GetXmlChildsOutTreeViewColumnsIDTemp + MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("Cont").Value + GetXmlChildsOutTreeViewCloumnLvTemp + "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<br/>");

                    //加入栏目 开始

                    //栏目表名(英文名)
                    string CloumnTable = MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("TabName").Value;

                    //栏目名
                    string CloumnName = "";

                    //栏目目录

                    string CloumnDir = "";


                    //栏目参数
                    string CloumnParam = "";

                    //栏目图片
                    string CloumnPic = "";

                    //添加的栏目类型 
                    string CloumnLX = "";//栏目类型, 0 为一般栏目 1 为新闻栏目 2 为其它栏目 3 为系统栏目

                    //栏目模板地址
                    string CloumnAddres = "";

                    //栏目的内容说明
                    string CloumnCont = "";

                    //生成HTML的基URL
                    string basecreateHTMLdri = "/html/";



                    ///加新栏目用于添加 此时是空的，在下面时程序将更新
                    TextData.SqlDataSave("insert into [CX_Column] (CX_Column_followid,CX_Column_Sort) values(" + ColumnID + ",0) ");
                    #region 开始处理栏目的各个参数
                    //当前栏目
                    DataTable CloumnFront = TextData.SqlataSet("select CX_Column_id from [CX_Column] order by CX_Column_id desc").Tables[0];
                    DataTable CloumnTables = new DataTable();
                    DataTable SenTable = new DataTable();

                    //栏目名
                    CloumnName = MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("Name").Value;

                    //栏目图片
                    CloumnPic = MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("pic").Value;

                    //栏目类型 
                    CloumnLX = MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("Lx").Value;

                    //栏目模板地址
                    CloumnAddres = MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("ItempAdderss").Value;

                    //栏目的内容说明Cont
                    CloumnCont = MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("Cont").Value;



                    //处理栏目参数
                    if (WantCloumnLv == 0)//如果是一级栏目
                    {
                        CloumnParam = "?CloumnID=" + CloumnFront.Rows[0][0].ToString();
                        CloumnDir = basecreateHTMLdri + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnName) + "/";

                    }
                    else
                    {
                        if (WantCloumnLv == 1)//如果是二级栏目
                        {
                            CloumnTables = TextThisSite.GetTopCloumns(CloumnFront.Rows[0][0].ToString(), WantCloumnLv);
                            ///得到指定级别的　栏目ＩＤ
                            CloumnParam = "?CloumnID=" + CloumnTables.Rows[0][0].ToString() + "&SendentID=" + CloumnFront.Rows[0][0].ToString();

                            CloumnDir = basecreateHTMLdri + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnTables.Rows[0]["CX_Column_Name"].ToString()) + "/" + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnName) + "/";
                        }
                        else
                        {
                            if (WantCloumnLv == 2)//如果是三级栏目
                            {

                                CloumnTables = TextThisSite.GetTopCloumns(CloumnFront.Rows[0][0].ToString(), WantCloumnLv);//得到一级栏目 Tab
                                SenTable = TextThisSite.GetTopCloumns(CloumnFront.Rows[0][0].ToString(), TextThisSite.SiteGetColumnLv(0, int.Parse(CloumnFront.Rows[0][0].ToString())) - 1);//得到2级栏目
                                CloumnParam = "?CloumnID=" + CloumnTables.Rows[0][0].ToString() + "&SendentID=" + SenTable.Rows[0][0].ToString() + "&CloumnID3=" + CloumnFront.Rows[0][0].ToString();
                                CloumnDir = basecreateHTMLdri + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnTables.Rows[0]["CX_Column_Name"].ToString()) + "/" + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(SenTable.Rows[0]["CX_Column_Name"].ToString()) + "/" + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnName) + "/";

                            }
                            else//如果还有下级栏目
                            {
                                if (WantCloumnLv > 2)
                                {
                                    CloumnParam = "";
                                    CloumnDir = "";

                                    for (int j = 3; j <= (WantCloumnLv); j++)
                                    {
                                        SenTable = TextThisSite.GetTopCloumns(CloumnFront.Rows[0][0].ToString(), TextThisSite.SiteGetColumnLv(0, int.Parse(CloumnFront.Rows[0][0].ToString())) - (j - 1));

                                        CloumnParam += "&CloumnID" + j + "=" + SenTable.Rows[0][0].ToString();
                                        //栏目目录
                                        CloumnDir = CloumnDir + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(SenTable.Rows[0]["CX_Column_Name"].ToString()) + "/";
                                    }

                                    CloumnTables = TextThisSite.GetTopCloumns(CloumnFront.Rows[0][0].ToString(), WantCloumnLv);//得到一级栏目 Tab
                                    SenTable = TextThisSite.GetTopCloumns(CloumnFront.Rows[0][0].ToString(), TextThisSite.SiteGetColumnLv(0, int.Parse(CloumnFront.Rows[0][0].ToString())) - 1);//得到2级栏目
                                    CloumnParam = "?CloumnID=" + CloumnTables.Rows[0][0].ToString() + "&SendentID=" + SenTable.Rows[0][0].ToString() + CloumnParam + "&CloumnID" + (WantCloumnLv + 1) + "=" + CloumnFront.Rows[0][0].ToString();
                                    //完成参数添加
                                    CloumnDir = basecreateHTMLdri + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnTables.Rows[0]["CX_Column_Name"].ToString()) + "/" + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(SenTable.Rows[0]["CX_Column_Name"].ToString()) + "/" + CloumnDir + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnName) + "/";
                                }
                            }
                        }
                    }

                    ///栏目参数
                    if (MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("itempParam").Value.Trim() != "")
                    {
                        CloumnParam = MyXml.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("itempParam").Value.Trim();
                    }

                    //栏目表名(英文名)
                    if (CloumnTable.Trim() == "")
                    {
                        CloumnTable = TextData.Guidx;

                    }

                    //如果没有找到index
                    if (CloumnDir.ToLower().IndexOf("index") == -1)
                    {
                        CloumnDir = CloumnDir + "index";
                    }

                    #endregion

                    //Response.Write(CloumnParam + "<br>");
                    //Response.Write(CloumnDir + "<br>");
                    //Response.Write("update [CX_Column] set " +
                    //                        "" +
                    //"" +
                    //"CX_Column_Name='" + CloumnName.Replace("'", "").Replace("'", "") + "'" +
                    //",CX_Column_Directory='" + CloumnDir.Replace("'", "").Replace("'", "") + "'" +
                    //",CX_Column_Explair='" + CloumnCont.Replace("'", "").Replace("'", "") + "'" +
                    //",CX_Column_ItemTemplate_Url='" + CloumnAddres.Replace("'", "").Replace("'", "") + "'" +
                    //",CX_Column_ItemTeplate_Parame='" + CloumnParam.Replace("'", "").Replace("'", "") + "'" +
                    //",CX_CX_Column_Tab_Name='" + CloumnTable.Replace("'", "").Replace("'", "") + "'" +
                    //",CX_Column_LX='" + CloumnLX.Replace("'", "").Replace("'", "") + "'" +
                    //",CX_Column_Pic='" + CloumnPic.Replace("'", "").Replace("'", "") + "'  where CX_Column_id=" + CloumnFront.Rows[0][0].ToString() + "<br/>");

                    // Response.Write("insert into [CX_Column] (CX_Column_followid,CX_Column_Sort) values(" + ColumnID + ",0) " + "<br/>");
                    // 开始创建表
                    if (TextThisSite.CreatTab(CloumnTable.Replace("'", "").Replace("'", "")))
                    {
                        if (CloumnLX.Replace("'", "").Replace("'", "") == "1")//如果是新闻栏目的列表 加入&page=$用于生成静态分页
                        {
                            TextData.SqlDataSave("update [CX_Column] set " +
                        "" +
                        "" +
                        "CX_Column_Name='" + CloumnName.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_Directory='" + CloumnDir.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_Explair='" + CloumnCont.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_ItemTemplate_Url='" + CloumnAddres.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_ItemTeplate_Parame='" + CloumnParam.Replace("'", "").Replace("'", "") + "&page=$'" +
                        ",CX_CX_Column_Tab_Name='" + CloumnTable.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_LX='" + CloumnLX.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_Pic='" + CloumnPic.Replace("'", "").Replace("'", "") + "' where CX_Column_id=" + CloumnFront.Rows[0][0].ToString() + "");
                        }
                        else
                        {
                            TextData.SqlDataSave("update [CX_Column] set " +
                        "" +
                        "" +
                        "CX_Column_Name='" + CloumnName.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_Directory='" + CloumnDir.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_Explair='" + CloumnCont.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_ItemTemplate_Url='" + CloumnAddres.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_ItemTeplate_Parame='" + CloumnParam.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_CX_Column_Tab_Name='" + CloumnTable.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_LX='" + CloumnLX.Replace("'", "").Replace("'", "") + "'" +
                        ",CX_Column_Pic='" + CloumnPic.Replace("'", "").Replace("'", "") + "' where CX_Column_id=" + CloumnFront.Rows[0][0].ToString() + "");
                        }
                    }

                    //开始添加新闻栏目
                    if (NewsParitemp != "")
                    {
                        //开始得到参数
                        string NewsParitempParame = "";

                        NewsParitempParame = "?id=$&lx=" + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnName.Replace("'", "").Replace("'", ""));
                        string sql = "";
                        sql = "insert into [CX_NEWS_Class] ( CX_NEWS_Class_Name,CX_Column_id,CX_NEWS_Class_Itemplitate_Address,  CX_NEWS_Class_Itemplitate_Parame,CX_NEWS_Class_CreateFielfront,CX_NEWS_Class_Directory)";
                        sql = sql + "values(";
                        sql = sql + " '" + CloumnName.Replace("'", "").Replace("'", "") + " 新闻分类'," + CloumnFront.Rows[0][0].ToString() + ", '" + NewsParitemp + "','" + NewsParitempParame + "', '" + Tong.CodeCharSet.ConvertHzToPz_Gb2312.Convert(CloumnName.Replace("'", "").Replace("'", "")) + "','" + CloumnDir.Replace("'", "").Replace("'", "").Replace("index", "") + "')";

                        if (CloumnLX.Replace("'", "").Replace("'", "") == "1")
                        {
                            TextData.SqlDataSave(sql);
                        }

                    }


                }
                GetXmlChildsOutTreeViewColumnsIDTemp += 1;
                GetXmlChildsOutTreeViewCloumnLvTemp++;
                GetXmlChildsOutTreeView(MyXml.DocumentElement.ChildNodes[i].OuterXml, WantCloumnLv, ColumnID, NewsParitemp);//得到这个XML文件下的所有子节点
                GetXmlChildsOutTreeViewCloumnLvTemp--;
            }
            catch (System.Xml.XmlException)//这里如是最下层就不进行下一次的调用自己了
            {
            }
        }
        return "";
    }

    /// <summary>
    /// 添加栏目事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (tt.Text == ""||tt.Text.IndexOf("清空栏目信息防此重复添加")!=-1)
        {
            if (XmlFileCont.Text.Replace("请添加XML数据", "") != "")
            {
                if (CheckBox1.Checked == true)//添加新闻栏目
                {

                    if (ColumnDid.Text.Trim() == "")//添加一级栏目
                    {
                        GetXmlChildsOutTreeView(XmlFileCont.Text.Replace("请添加XML数据", ""), 0, 0, Newsitemplate.Text.Trim());
                        tt.Text = "添加一级栏目成功 并成功添加新闻分类";
                    }
                    else
                    {
                        tt.Text = "";
                        for (int i = 0; i < ColumnDid.Text.Trim().Split(',').Length; i++)
                        {

                            if (ColumnDid.Text.Trim().Split(',')[i].Trim() != "")
                            {
                                GetXmlChildsOutTreeView(XmlFileCont.Text.Replace("请添加XML数据", ""), (TextThisSite.SiteGetColumnLv(0, int.Parse(ColumnDid.Text.Trim().Split(',')[i])) + 1), int.Parse(ColumnDid.Text.Trim().Split(',')[i]), Newsitemplate.Text.Trim());

                                tt.Text += "添加" + ColumnDid.Text.Trim().Split(',')[i].Trim() + "成功 并成功添加新闻分类<br/>";

                            }

                        }
                    }
                }
                else//同时添加新闻栏目
                {
                    if (ColumnDid.Text.Trim() == "")//添加一级栏目
                    {
                        GetXmlChildsOutTreeView(XmlFileCont.Text.Replace("请添加XML数据", ""), 0, 0, "");
                        tt.Text = "添加一级栏目成功";
                    }
                    else
                    {
                        tt.Text = "";
                        for (int i = 0; i < ColumnDid.Text.Trim().Split(',').Length; i++)
                        {

                            if (ColumnDid.Text.Trim().Split(',')[i].Trim() != "")
                            {
                                GetXmlChildsOutTreeView(XmlFileCont.Text.Replace("请添加XML数据", ""), (TextThisSite.SiteGetColumnLv(0, int.Parse(ColumnDid.Text.Trim().Split(',')[i])) + 1), int.Parse(ColumnDid.Text.Trim().Split(',')[i]), "");

                                tt.Text += "添加" + ColumnDid.Text.Trim().Split(',')[i].Trim() + "成功<br/>";

                            }

                        }
                    }
                }
                tt.Text += "清空栏目信息防此重复添加";
                XmlFileCont.Text = "请添加XML数据";
                Div.InnerHtml = "";
            }
        }
        else
        {
            tt.Text = "不能重复添加";
            Div.InnerHtml = "";
        }
    }

    /// <summary>
    /// 输出成XML添加文件的操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string ColumnNames = CX_ColumnSNames.Text.Trim().Replace("\r", "").Replace("\n", "");
        string[] ColumnNamesNUms = ColumnNames.Split(',');//得到输出的栏目名,以 ,格开的栏目名

        string XMlFileDoc = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n\t<AddColumns>";

        //循环得到栏目名 输出 添加的XML文件
        for (int i = 0; i < ColumnNamesNUms.Length; i++)
        {
            if (ColumnNamesNUms[i].Replace("\r", "").Replace("\n", "").Trim() != "")
            {
                XMlFileDoc += "\t\t<Column Name=\"" + Server.HtmlEncode(ColumnNamesNUms[i].Replace("\r", "").Replace("\n", "").Trim()) + "\" Lx=\"" + int.Parse(ColumnLX.Text.Trim()) + "\" dir=\"\" pic=\"\" ItempAdderss=\"" + ColnumsItemp.Text.Trim() + "\" itempParam=\"\" TabName=\"\" Cont=\"" + Server.HtmlEncode(ColumnNamesNUms[i].Replace("\r", "").Replace("\n", "").Trim()) + "的介绍\"> </Column>\r\n";
            }
        }
        XMlFileDoc = XMlFileDoc + "\r\n\t</AddColumns>";

        if (ColumnNames != "")
        {
            XmlFileCont.Text = XMlFileDoc;//输出
            Div.InnerHtml = "<script>alert('以成功执行得到XML数据 \\r\\n请确在XML文件内容文本框内确认内容后添加');</script>";
            CX_ColumnSNames.Text = "";
        }

    }
}
